
package datolib;

import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;

public class XmlMessage {	

	private String xmlString;
	private XmlMessageType type;
	private Element element;
	
	public XmlMessage(String msg) {
		this.xmlString = msg;
		this.element = null;
		this.type = null;
		this.read();
	}
	
	public void print() {
		System.out.println(this.xmlString);
	}

	public XmlMessageType type() {
		return this.type;
	}
	
	public boolean isLAP() {
		return (this.type == XmlMessageType.LAP);
	}
	
	public boolean isGUP() {
		return (this.type == XmlMessageType.GUP);
	}

	public LapMessage toLap() {
		
		String application = null;
		String platform = null;
		String username = null;
		String password = null;
		
		
		if (this.element.hasAttribute("application")) {
			application = this.element.getAttribute("application");
		}
		
		if (this.element.hasAttribute("platform")) {
			platform = this.element.getAttribute("platform");
		}
		
		if (this.element.hasAttribute("username") && this.element.hasAttribute("password")) {
			username = this.element.getAttribute("username");
			password = this.element.getAttribute("password");
		}
		
		if (username == null) {
			return new LapMessage(application, platform);
		} else {
			return new LapMessage(application, platform, username, password);
		}
		
	}
	
	public TevMessage toTev() {
	
		String function = null;
		
		if (!this.element.hasAttribute("function")) {
			return null;
		}
		
		function = this.element.getAttribute("function");
		
		TevMessage message = new TevMessage(function);
		
		this.readTevParameters(message);
		
		message.load();
		
		return message;
	
	}

	private void readTevParameters(TevMessage message) {

		NodeList params = this.element.getElementsByTagName("param");
		
		for (int i = 0; i < params.getLength(); ++i) {
			
			if (params.item(i).getNodeType() != Node.ELEMENT_NODE) {
				continue;
			}
			
			Element param = (Element)params.item(i);
			
			if (!param.getTagName().equals("param")) {
				continue;
			}
			
			String type = null;
			String value = null;
			
			if (!param.hasAttribute("type")) {
				continue;
			}
			
			if (!param.hasAttribute("value")) {
				value = params.item(i).getTextContent();
			} else {
				value = param.getAttribute("value");
			}
			
			type = param.getAttribute("type");			
			
			message.addParameter(type, value);
				
		}
	}

	private void read() {
		
		try {
		
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
      
            
			Document xml = db.parse(new InputSource(new StringReader(this.xmlString)));
            
        	this.element = xml.getDocumentElement();
        	
        	this.getType(element.getTagName());
        	
		
		} catch (Exception e) {
			
		}
	}
	
	private void getType(String tagName) {
		if (tagName.equals(LapMessage.TAG)) {
			this.type = XmlMessageType.LAP;
		} else if (tagName.equals("gup")) {
			this.type = XmlMessageType.GUP;
		} else if (tagName.equals("sai")) {
			this.type = XmlMessageType.SAI;
		} else if (tagName.equals("tev")) {
			this.type = XmlMessageType.TEV;
		} else if (tagName.equals("ext")) {
			this.type = XmlMessageType.EXT;
		}
	}

}
